#include <pthread.h>
#include <stdio.h>

static pthread_mutex_t counter_mutex = PTHREAD_MUTEX_INITIALIZER;
static int sum=0;

static void *sum_func(void *arg)
{
    pthread_mutex_lock(&counter_mutex);
    sum++;
    pthread_mutex_unlock(&counter_mutex);
}


int main(int argc, char const *argv[])
{
    pthread_t thread[30000];
    for ( int i = 0; i < 30000; i++)
    {
         pthread_create(&thread[i], NULL,  sum_func, NULL);
    }
    
    
    for ( int i = 0; i < 30000; i++)
    {
         pthread_join( thread[i],  NULL);
    }
    printf("sum = %d\n", sum);
    
    pthread_mutex_destroy(&counter_mutex);
    return 0;
}
